package ristorante;

import java.awt.EventQueue;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.freixas.jcalendar.*;

public class PrenotazioniView extends JFrame {

	private JPanel contentPane;
	private JTable table;
	public ResultSet rs;
	Prenotazioni prenotazioni = new Prenotazioni();
	DefaultTableModel model = new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Id", "Nome", "Data", "N. persone", "Telefono"
			}
		);
	private JTextField txtNome;
	private JTextField txtNPersone;
	int idmod = -1;
	long timestamp;
	private JTextField txtDataOra;
	private JTextField txtTelefono;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PrenotazioniView frame = new PrenotazioniView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public PrenotazioniView() throws ClassNotFoundException, SQLException {
		setTitle("Prenotazioni");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 829, 452);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		final JPanel panel = new JPanel();
		final JButton btnAggiungi = new JButton("Aggiungi");

		
		JButton btnModifica = new JButton("Modifica prenotazione");
		btnModifica.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtNome.setText(table.getValueAt(table.getSelectedRow(), 1).toString());
				txtDataOra.setText(table.getValueAt(table.getSelectedRow(), 2).toString());
				txtNPersone.setText(table.getValueAt(table.getSelectedRow(), 3).toString());
				txtTelefono.setText(table.getValueAt(table.getSelectedRow(), 4).toString());

				btnAggiungi.setText("Modifica");
				panel.setVisible(true);
				idmod = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString());
			}
		});
		panel.setVisible(false);
		JScrollPane scrollPane = new JScrollPane();
		
		table = new JTable();

		table.setModel(model);
		scrollPane.setViewportView(table);
		
		JButton btnAggiungiPrenotazione = new JButton("Aggiungi prenotazione");

		btnAggiungiPrenotazione.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtNome.setText("");
				txtDataOra.setText("");
				txtNPersone.setText("");
				txtTelefono.setText("");

				btnAggiungi.setText("Aggiungi");
				panel.setVisible(true);
			}
		});
		
		JButton btnRimuoviPrenotazione = new JButton("Rimuovi Prenotazione");
		btnRimuoviPrenotazione.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					prenotazioni.elimina(Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString()));
					aggiornatabella();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(10)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 438, GroupLayout.PREFERRED_SIZE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(118)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(btnAggiungiPrenotazione, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(btnModifica, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(btnRimuoviPrenotazione, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 359, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(53, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(18)
					.addComponent(btnAggiungiPrenotazione)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnModifica)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnRimuoviPrenotazione)
					.addGap(11)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
					.addGap(150))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(3)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE))
		);
		SpringLayout sl_panel = new SpringLayout();
		panel.setLayout(sl_panel);
		
		JLabel lblNome = new JLabel("Nome");
		sl_panel.putConstraint(SpringLayout.NORTH, lblNome, 10, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, lblNome, 10, SpringLayout.WEST, panel);
		panel.add(lblNome);
		
		txtNome = new JTextField();
		sl_panel.putConstraint(SpringLayout.NORTH, txtNome, -2, SpringLayout.NORTH, lblNome);
		sl_panel.putConstraint(SpringLayout.WEST, txtNome, 87, SpringLayout.EAST, lblNome);
		panel.add(txtNome);
		txtNome.setColumns(10);
		
		JLabel lblDataEOra = new JLabel("Data e ora");
		sl_panel.putConstraint(SpringLayout.NORTH, lblDataEOra, 10, SpringLayout.SOUTH, lblNome);
		sl_panel.putConstraint(SpringLayout.WEST, lblDataEOra, 0, SpringLayout.WEST, lblNome);
		panel.add(lblDataEOra);
		
		txtDataOra = new JTextField();
		sl_panel.putConstraint(SpringLayout.NORTH, txtDataOra, -2, SpringLayout.NORTH, lblDataEOra);
		sl_panel.putConstraint(SpringLayout.WEST, txtDataOra, 62, SpringLayout.EAST, lblDataEOra);
		panel.add(txtDataOra);
		txtDataOra.setColumns(10);
		
		JLabel lblNPersone = new JLabel("N. Persone");
		sl_panel.putConstraint(SpringLayout.NORTH, lblNPersone, 9, SpringLayout.SOUTH, lblDataEOra);
		sl_panel.putConstraint(SpringLayout.WEST, lblNPersone, 0, SpringLayout.WEST, lblNome);
		panel.add(lblNPersone);
		
		txtNPersone = new JTextField();
		sl_panel.putConstraint(SpringLayout.WEST, txtNPersone, 56, SpringLayout.EAST, lblNPersone);
		sl_panel.putConstraint(SpringLayout.EAST, txtNPersone, -10, SpringLayout.EAST, panel);
		sl_panel.putConstraint(SpringLayout.EAST, txtNome, 0, SpringLayout.EAST, txtNPersone);
		sl_panel.putConstraint(SpringLayout.NORTH, txtNPersone, 6, SpringLayout.SOUTH, txtDataOra);
		panel.add(txtNPersone);
		txtNPersone.setColumns(10);
		btnAggiungi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if (idmod == -1){
					prenotazioni.aggiungi(txtNome.getText(), timestamp, Integer.parseInt(txtNPersone.getText()), txtTelefono.getText());
					} else {
						prenotazioni.modifica(idmod, txtNome.getText(), timestamp, Integer.parseInt(txtNPersone.getText()), txtTelefono.getText());
						idmod = -1;
					}
					aggiornatabella();
					txtNome.setText("");
					txtDataOra.setText("");
					txtNPersone.setText("");
					txtTelefono.setText("");
				} catch (NumberFormatException | SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				panel.setVisible(false);
			}
		});
		panel.add(btnAggiungi);
		
		JButton btnAnnulla = new JButton("Annulla");
		sl_panel.putConstraint(SpringLayout.WEST, btnAnnulla, 212, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.NORTH, btnAggiungi, 0, SpringLayout.NORTH, btnAnnulla);
		sl_panel.putConstraint(SpringLayout.EAST, btnAggiungi, -45, SpringLayout.WEST, btnAnnulla);
		btnAnnulla.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtNome.setText("");
				txtDataOra.setText("");
				txtTelefono.setText("");
				txtNPersone.setText("");
				idmod = -1;
				panel.setVisible(false);
			}
		});
		panel.add(btnAnnulla);
		
		
		JButton button = new JButton("...");
		sl_panel.putConstraint(SpringLayout.EAST, txtDataOra, -6, SpringLayout.WEST, button);
		sl_panel.putConstraint(SpringLayout.NORTH, button, 0, SpringLayout.NORTH, lblDataEOra);
		sl_panel.putConstraint(SpringLayout.WEST, button, 315, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, button, 0, SpringLayout.SOUTH, lblDataEOra);
		sl_panel.putConstraint(SpringLayout.EAST, button, -10, SpringLayout.EAST, panel);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JCalendar cal = new JCalendar(JCalendar.DISPLAY_DATE | JCalendar.DISPLAY_TIME,true);
				cal.setLocale(new Locale("it", "IT"));
				JOptionPane.showMessageDialog(getParent(), cal, "Selezionare la data", JOptionPane.PLAIN_MESSAGE);
				timestamp = cal.getDate().getTime();
				txtDataOra.setText(cal.getDate().toString());
			}
		});
		panel.add(button);
		
		JLabel lblTelefono = new JLabel("Telefono");
		sl_panel.putConstraint(SpringLayout.NORTH, lblTelefono, 9, SpringLayout.SOUTH, lblNPersone);
		sl_panel.putConstraint(SpringLayout.WEST, lblTelefono, 0, SpringLayout.WEST, lblNome);
		panel.add(lblTelefono);
		
		txtTelefono = new JTextField();
		sl_panel.putConstraint(SpringLayout.NORTH, btnAnnulla, 9, SpringLayout.SOUTH, txtTelefono);
		sl_panel.putConstraint(SpringLayout.NORTH, txtTelefono, 6, SpringLayout.SOUTH, txtNPersone);
		sl_panel.putConstraint(SpringLayout.WEST, txtTelefono, 72, SpringLayout.EAST, lblTelefono);
		sl_panel.putConstraint(SpringLayout.EAST, txtTelefono, 0, SpringLayout.EAST, txtNome);
		panel.add(txtTelefono);
		txtTelefono.setColumns(10);
		contentPane.setLayout(gl_contentPane);
		aggiornatabella();
	}
	
	public void aggiornatabella() throws SQLException{
	    rs = prenotazioni.stat.executeQuery("select * from Prenotazioni");
		for (int i = model.getRowCount() - 1; i >= 0; i--) {
		    model.removeRow(i);
		}		
		while(rs.next())
		{
			model.addRow(new Object[]{ rs.getString(1), rs.getString(2), new Date(Long.parseLong(rs.getString(3))).toString(), rs.getString(4), rs.getString(5)});
		}
		
		rs.close();
	}
}